import { observable, action,toJS } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
// const {TableStore} = WeaTableNew;
import * as Apis from "../apis/instock"
import {Modal,message} from "antd"
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export  class InstockHisStore{
    title = "查看入库单";
    @observable instockForm = new WeaForm();
    
    @observable visible = false;
    @observable basicInfo = [];
    @observable subInfo = {};
    @observable canEdit = true;
    @observable rightMenu = [];
    instockid = "";
    @observable subListData = [];
    @observable instockRemind;
    @observable batchBrowser;

    @action 
    handleDialog = (bool,params={} ,state,operate) =>{
        this.visible = bool;
        if(bool){
            let type = {viewtype:"view"}
            if(operate == "0"){
                if(state == "1"){  //已入库
                    this.title =  getLabel('385213',"查看入库单");
                    this.rightMenu= [];
                    type.viewtype = "view";
                }else if(state == "-2"){  //已收回
                    this.title = getLabel('83575',"修改入库单");
                    this.rightMenu= [{
                        "isTop": "1",
                        "menuFun": "",
                        "menuIcon": "icon-coms-Upload-successfully",
                        "menuName": getLabel(615,"提交"),
                        "type": "BTN_SAVE"
                    }];
                    type.viewtype = "edit";
                }else if(state == "-1"){  //已作废
                    this.title = getLabel('385214',"重新修改入库单");
                    this.rightMenu= [{
                        "isTop": "1",
                        "menuFun": "",
                        "menuIcon": "icon-coms-Reopen",
                        "menuName": getLabel('82690',"重新申请"),
                        "type": "BTN_REJECT"
                    }];
                    type.viewtype = "view";
                }else{  //待验
                    this.title = getLabel('385213',"查看入库单");
                    this.rightMenu= [{
                        "isTop": "1",
                        "menuFun": "",
                        "menuIcon": "icon-coms-Need-feedback",
                        "menuName": getLabel('18666',"收回"),
                        "type": "BTN_BACK"
                    }];
                    type.viewtype = "view";
                }
            }else if(operate == "2"){
                this.title = getLabel('83586',"重新填写入库单");
                this.rightMenu= [{
                    "isTop": "1",
                    "menuFun": "",
                    "menuIcon": "icon-coms-Upload-successfully",
                    "menuName": getLabel(615,"提交"),
                    "type": "BTN_SAVE"
                }];
                type.viewtype = "edit";
            }
            this.initInstockInfo({...params,...type});
        }
    }
    //请求数据
    initInstockInfo =(params ={})=>{
        this.instockForm = new WeaForm();
        this.instockid = params.instockid;
        this.subListData = [];
        Apis.getInstockInfo(params).then(data=>{
            this.basicInfo = data.mainTableInfo;
            this.subInfo = data.subTableInfo;
            this.subListData = data.subTableInfo.columnDefine;
            this.canEdit = data.subTableInfo.needAdd || false;
            this.batchBrowser = data.batchBrowser;
            // console.log(data.subTableInfo.needAdd,this.canEdit,"this.canEdit")
            //根据高级搜索条件初始化form
            this.instockForm && !this.instockForm.isFormInit && this.instockForm.initFormFields(data.mainTableInfo);
        })
    }

     //回收
     doRejectInstock =(params={},cptInstockStore) =>{
         let that = this;
        Apis.doReject(params).then(data=>{
            if(data.sms){
                Modal.info({
                    title: getLabel(15172,'系统提示'),
                    content: getLabel('385216',"回收成功！"),
                    onOk() {
                        that.handleDialog(true,{instockid:params.instockid},-2,0);
                        cptInstockStore.doSearch();
                    }
                 });
            }else{
                Modal.error({ 
                    title: getLabel(15172,'系统提示'),
                    content: getLabel('385217',"回收失败！"),
                })
            }
        })
    }
//编辑历史记录保存字列表数据
    saveSubListData=(data)=>{
        this.subListData = data;
    }
    //提交入库单
    doInstock=(cptInstockStore)=>{
        this.instockForm.validateForm().then( f =>{
            if(f.isValid&&this.instockRemind.showErrors()){
                let par = {instockid:this.instockid};
                let subfields = toJS(this.subListData);
                let fields = this.instockForm.getFormParams();
                //校验购置日期和入库日期
                if(fields['stockindate_gz']>fields['stockindate']){
                    message.error(getLabel('83582',"入库日期不能小于购置日期！"));
                    return false;
                }
                par = {...par,...fields};
                par.subfields = JSON.stringify(subfields);
                Apis.doInstock(par).then(data=>{
                    if(data.result){
                        message.success(getLabel('385306',"入库单提交成功"));
                        this.subListData = [];
                        this.basicInfo = [];
                        this.subInfo = {};
                        cptInstockStore.doSearch();
                        this.handleDialog(false);
                        //入库验收左侧菜单统计
                        loadLeftMenuCount();
                    }else{
                        Modal.warning({
                            title: getLabel(15172, '系统提示'),
                            content: data.tip,
                        });
                    }
            })
            }else{
                f.showErrors();
                this.instockRemind.showErrors();
            }
        });
    }

    check_form = ()=>{
        let checkstr = 'stockindate_gz,checkerid,stockindate'
        let subfields = toJS(this.subListData);
        let fields = this.instockForm.getFormParams();
        let checkitem = checkstr.split(",");
        let bool = true;
         //先校验购置日期和入库日期 ，在校验必填
         if(fields['stockindate_gz']>fields['stockindate']){
            // Modal.warning({
            //     title: getLabel(15172,'系统提示'),
            //     content: getLabel('83582',"入库日期不能小于购置日期！"),
            // });
            message.error(getLabel('83582',"入库日期不能小于购置日期！"));
            return false;
        }else{
            for(let i=0 ; i<checkitem.length ; i++) { 
                let checkid = checkitem[i];
                if(checkid){
                    if(!(fields[checkid])){
                        bool=false;
                    }
                }
            }
            subfields.map((item) =>{
                if(!(item.capitalid && item.capitalspec && item.price && item.capitalnum)){
                    bool=false;
                }
            })
            return bool;
        }
    }

}